﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskListManager
{
   [Serializable()]
   class UserList
   {
      protected List<User> m_sUsers;

      public UserList()
      {
         m_sUsers = new List<User>();
      }

      public int GetUserCount()
      {
         return m_sUsers.Count();
      }

      public string GetUserAtIndex(int lIdx)
      {
         string strName = "";
         if ((lIdx >= 0) && (lIdx < m_sUsers.Count()))
         {
            strName = m_sUsers[lIdx].UserName;
         }
         return strName;
      }

      protected int GetNextAvailableUserId()
      {
         int lHighest = 0;
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserId > lHighest)
            {
               lHighest = m_sUsers[i].UserId;
            }
         }
         return (lHighest + 1);
      }

      protected int GetIndexOfUserWithId(int lUserId)
      {
         int lIdx = -1;
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserId == lUserId)
            {
               lIdx = i;
               break;
            }
         }
         return lIdx;
      }

      public void RenameUser(int lUserId, string strName)
      {
         int lUserIdx = GetIndexOfUserWithId(lUserId);
         if (lUserIdx != -1)
         {
            m_sUsers[lUserIdx].UserName = strName;
         }
      }

      public int AddUser(string strUserName)
      {
         User u = new User();
         u.UserId = GetNextAvailableUserId();
         u.UserName = strUserName;
         m_sUsers.Add(u);
         return u.UserId;
      }

      public string GetUserName(int lIdent)
      {
         string strName = "";
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserId == lIdent)
            {
               strName = m_sUsers[i].UserName;
               break;
            }
         }
         return strName;
      }

      public int GetUserId(string strName)
      {
         int lIdent = -1;
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserName.Equals(strName, StringComparison.OrdinalIgnoreCase) == true)
            {
               lIdent = m_sUsers[i].UserId;
               break;
            }
         }
         return lIdent;
      }

      public void RemoveUser(string strName)
      {
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserName.Equals(strName, StringComparison.OrdinalIgnoreCase) == true)
            {
               m_sUsers.Remove(m_sUsers[i]);
               break;
            }
         }
      }

      public void RemoveUser(int lUserId)
      {
         for (int i = 0; i < m_sUsers.Count; i++)
         {
            if (m_sUsers[i].UserId == lUserId)
            {
               m_sUsers.Remove(m_sUsers[i]);
               break;
            }
         }
      }

      public bool UserExists(string strName)
      {
         bool bRet = false;
         foreach (User u in m_sUsers)
         {
            if (u.UserName.Equals(strName, StringComparison.OrdinalIgnoreCase))
            {
               bRet = true;
               break;
            }
         }
         return bRet;
      }

      public bool UserExists(int lUserId)
      {
         bool bRet = false;
         foreach (User u in m_sUsers)
         {
            if (u.UserId == lUserId)
            {
               bRet = true;
               break;
            }
         }
         return bRet;
      }
   }
}
